from common_data import *
from permission import *
from twisted.web import xmlrpc
from twisted.python import log

class XMLRPC_XMLRPCServerConfig( xmlrpc.XMLRPC ):
    _permissions = {
        'listXMLRPCServerConfig':               PERMISSION_REQUIREMENT_GLOBAL_ADMIN,
        'setXMLRPCServerHTTPPort':              PERMISSION_REQUIREMENT_GLOBAL_ADMIN,
        'setXMLRPCServerConfigRefreshInterval': PERMISSION_REQUIREMENT_GLOBAL_ADMIN,
        'setXMLRPCServerEnableSSL':             PERMISSION_REQUIREMENT_GLOBAL_ADMIN,
        'setXMLRPCServerSSLCertificate':        PERMISSION_REQUIREMENT_GLOBAL_ADMIN,
        'setXMLRPCServerSSLKey':                PERMISSION_REQUIREMENT_GLOBAL_ADMIN,
    }
    
    def __init__(self):
        """ load config
        """
        log.msg( 'XMLRPCServerConfig resource init' )
        xmlrpc.XMLRPC.__init__(self)

    def xmlrpc_setXMLRPCServerHTTPPort(self, new_port):
        """ define the listen port
        """
        if not int( new_port ):
            raise xmlrpc.Fault( 123, 'Invalid arguments' )
            
        current_config = self.getLatestXMLRPCServerConfig()
        
        if current_config:
            current_config.http_port = int( new_port )
            current_config.updateModifiedTime()
            
        return True
    
    xmlrpc_setXMLRPCServerHTTPPort.signature = [['bool','int']]
    
    def xmlrpc_setXMLRPCServerConfigRefreshInterval(self, new_interval ):
        """ define the interval between config refresh
        """
        if not int( new_interval ):
            raise xmlrpc.Fault( 123, 'Invalid arguments' )
        
        if int( new_interval ) < 1:
            raise xmlrpc.Fault( 123, 'new interval is too small' )
            
        current_config = self.getLatestXMLRPCServerConfig()
        
        if current_config:
            current_config.config_refresh_interval = int( new_interval )
            current_config.updateModifiedTime()
            
        return True
    
    xmlrpc_setXMLRPCServerConfigRefreshInterval.signature = [['bool','int']]
    
    def xmlrpc_setXMLRPCServerEnableSSL(self, new_setting ):
        """ define if server should use ssl
        """
        if not int( new_setting ):
            raise xmlrpc.Fault( 123, 'Invalid arguments' )
            
        current_config = self.getLatestXMLRPCServerConfig()
        
        if current_config:
            current_config.enable_ssl = ( int( new_setting ) == 1 and True ) or False
            current_config.updateModifiedTime()
            
        return True
    
    xmlrpc_setXMLRPCServerEnableSSL.signature = [['bool','int']]
    
    def xmlrpc_setXMLRPCServerSSLKey(self, new_key ):
        """ define location of ssl key when ssl is enabled
        """
        if not str( new_key ):
            raise xmlrpc.Fault( 123, 'Invalid arguments' )
            
        current_config = self.getLatestXMLRPCServerConfig()
        
        if current_config:
            current_config.ssl_key = str( new_key )
            current_config.updateModifiedTime()
            
        return True
    
    xmlrpc_setXMLRPCServerSSLKey.signature = [['bool','string']]
    
    def xmlrpc_setXMLRPCServerSSLCertificate(self, new_cert ):
        """ define location of ssl certificate when ssl is enabled
        """
        if not str( new_cert ):
            raise xmlrpc.Fault( 123, 'Invalid arguments' )
            
        current_config = self.getLatestXMLRPCServerConfig()
        
        if current_config:
            current_config.ssl_certificate = str( new_cert )
            current_config.updateModifiedTime()
            
        return True
    
    xmlrpc_setXMLRPCServerSSLCertificate.signature = [['bool','string']]

    def xmlrpc_listXMLRPCServerConfig(self):
        """ return a dict of xmlrpc server options
        """
            
        current_config = self.getLatestXMLRPCServerConfig()
        
        if current_config:        
            return self.xmlConfigResultToDict( current_config )
        else:
            return False
        
    xmlrpc_listXMLRPCServerConfig.signature = [['array']]
        
    def xmlConfigResultToDict(self, config ):
            result = dict()
            result['id'] = int( config.id )
            result['http_port'] = int( config.http_port )
            result['config_refresh_interval'] = int( config.config_refresh_interval )
            result['enable_ssl'] = config.enable_ssl
            result['ssl_key'] = str( config.ssl_key )
            result['ssl_certificate'] = str( config.ssl_certificate )
            
            result['creation_time'] = xmlrpc.DateTime( config.creation_time ).value
            result['modified_time'] = xmlrpc.DateTime( config.modified_time ).value
            
            return result
        
    def getLatestXMLRPCServerConfig(self):
        
        try:
            current_config = XMLRPCServerConfig.select( orderBy=XMLRPCServerConfig.q.creation_time ).reversed().limit(1).getOne()
        except:
            log.err( 'could not retrieve current xmlrpc server config' )
            log.err()
            return False
        
        return current_config
        